<?php
 include_once('config/config.php');
 // 查询有几个部门
 $sql = "SELECT * FROM pre_department;";
 
 $arr_prename = all($sql);
 
 $datas = [];
 
 // 二维数组变成一维
 foreach($arr_prename as $item){
	$datas[] = $item['name'];
 }
 
 // 判断有没有表单提交过来
 if($_POST){
 	// 接收全部参数
 	$params = $_POST;
	// 封装一个数组
	$data = [
		'name'=>$params['name'],
	];
	// 判断是否有该部门
	if(in_array($params['name'],$datas)){
		ShowMsg('已有该部门');
	}else{
		// 添加数据
		$result = insert('department',$data);
		if($result){
			ShowMsg('新增成功','departmentlist.php');
			exit;
		}else{
			ShowMsg('新增失败');
		}
	}
 }
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<?php include_once('common/meta.php'); ?>
	</head>
	<style>
		
	</style>
	<body>

		<?php include_once('common/header.php'); ?>

		<?php include_once('common/sidebar.php'); ?>

		<div class="content">
			<div class="header">
				<h1 class="page-title">新增部门</h1>
			</div>
			<ul class="breadcrumb">
				<li><a href="index.php">Home</a> <span class="divider">/</span></li>
				<li class="active">Index</li>
			</ul>

			<div class="container-fluid">
				<div class="row-fluid">

					<div class="btn-toolbar">
						<button class="btn btn-primary" onClick="location='departmentlist.php'"><i class="icon-list"></i>部门列表</button>
						<div class="btn-group">
						</div>
					</div>

					<div class="well">
						<div id="myTabContent" class="tab-content">
							<div class="tab-pane active in" id="home">
								<form method="post" enctype="multipart/form-data">
									<!-- 部门名称 -->
									<label>部门名称</label>
									<input type="text" name="name" class="input-xxlarge" placeholder="请输入添加部门名称" required />
									<!-- 部门 -->
									<label>已有部门</label>
									<select>
										<?php foreach($arr_prename as $item){ ?>
										   <option selected disabled value="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
										<?php } ?>
									</select>
									
									<br />
									<!-- 提交 -->
									<button type="submit" class="btn btn-primary">提交</button>
								</form>
							</div>
						</div>
					</div>
					<?php include_once('common/footer.php'); ?>

				</div>
			</div>
		</div>

		<?php include_once('common/script.php'); ?>

	</body>
</html>
